package com.hrt.nacos.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Auther: hrt
 * @Date: 2022/2/10
 * 读取nacos的配置
 */
public class GetNacosConfig {

    public static void main(String[] args) {
        try {
            String serverAddr = "localhost";
            String dataId = "test_nacos_client.properties";
            String group = "defualt_group";
            Properties properties = new Properties();
            properties.put("serverAddr", serverAddr);
            ConfigService configService = NacosFactory.createConfigService(properties);
            String content = configService.getConfig(dataId, group, 5000);
            String[] split = content.split("\n");
            Map<String,String> map=new HashMap<>();
            for(int i=0;i<split.length;i++){
                String key=split[i].split("=")[0];
                String value=split[i].split("=")[1];
                map.put(key,value);
            }
            System.out.println(map.toString());
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
